package yunquan.wang.base.utils;

import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;

import yunquan.wang.base.service.ServiceException;
import yunquan.wang.base.spring.SpringContextHolder;

public class ConvertUtils {
	private static final ConversionService conversionService =SpringContextHolder.getBean(ConversionService.class);
	
	public ConversionService getInstance( )
	{
		return conversionService;
	}
	
	
	public static  Object convert(Object source,Class classtype)
	{
		if(conversionService.canConvert(source.getClass(), classtype))
		{
	return	conversionService.convert(source, classtype);
		}
		else
		{
			throw new ServiceException(source.getClass()+" can not be cast to"+classtype);
		}
	}
}
